import os
import re

# 中文提示到英文提示的映射
replace_map = {
    # 之前已有
    "文档未找到": "Document not found",
    "无法进行越权操作！": "Unauthorized operation!",
    "栏目未找到": "Channel not found",
    "参数不能为空": "Parameter cannot be empty",
    "发生错误:": "An error occurred:",
    "提交成功！请等待审核!": "Submission successful! Please wait for review!",
    "发布成功！": "Published successfully!",
    "Request error": "Request error",
    "parameter is incorrect": "Parameter is incorrect",
    "删除文档失败": "Failed to delete document",
    "删除文档成功": "Document deleted successfully",
    "未找到指定模型": "Specified model not found",
    "未找到指定文章": "No specified article found",
    "请先登录": "Please login first",
    "未找到指定栏目": "Specified channel not found",
    "未找到登录用户信息": "User login information not found",
    "操作失败": "Operation failed",
    "点赞成功！": "Vote successful!",
    "请选择栏目": "Please select a channel",
    "积分必须大于": "Score must be greater than",
    "标题不能为空": "Title cannot be empty",
    "标题长度限制在3~100个字符": "Title length must be between 3 and 100 characters",
    "内容不能为空": "Content cannot be empty",
    "栏目不允许投稿": "Channel does not allow contributions",
    "请求成功": "Request successful",
    "未找到指定的文档": "Specified document not found",

    "请勿重复收藏": "Do not duplicate collection",
    "数据未找到": "Data not found",
    "收藏失败": "Collection failed",
    "收藏成功": "Collection successful",
    "移除Collection失败": "Failed to remove collection",
    "移除Collection成功": "Collection removed successfully",
    "邮箱不能为空": "Email cannot be empty",
    "邮箱格式不正确": "Invalid email format",
    "反馈Content cannot be empty": "Feedback content cannot be empty",
    "反馈分类不能为空": "Feedback category cannot be empty",
    "反馈分类错误": "Invalid feedback category",
    "反馈提交成功": "Feedback submitted successfully",
    "反馈提交失败": "Feedback submission failed",
    "参数错误": "Parameter error",
    "反馈不存在": "Feedback not found",
    "发送频率过高": "Send too frequently",
    "已注册": "Already registered",
    "已被占用": "Already occupied",
    "未注册": "Not registered",
    "发送成功": "Send successful",
    "发送失败": "Send failed",
    "验证码不正确": "Verification code incorrect",
    "成功": "Success",

    "单页未找到": "Page not found",
    "歌单不存在": "Playlist not found",
    "歌单名称不能为空": "Playlist name cannot be empty",
    "创建成功": "Created successfully",
    "创建失败": "Creation failed",
    "更新成功": "Updated successfully",
    "更新失败": "Update failed",
    "删除成功": "Deleted successfully",
    "删除失败": "Deletion failed",
    "添加成功": "Added successfully",
    "添加失败": "Add failed",
    "移除成功": "Removed successfully",
    "移除失败": "Remove failed",
    "音乐已存在歌单中": "Music already exists in playlist",
    "歌单不存在或无权限": "Playlist not found or no permission",
    "已收藏该歌曲": "Music already collected",
    "取消Collection successful": "Uncollect successful",
    "取消Collection failed": "Uncollect failed",
    "手机号不正确": "Invalid mobile number",
    "请在后台插件管理安装短信验证插件": "Please install SMS verification plugin in backend",
    "Send too frequently": "Send too frequently",
    "Send successful": "Send successful",
    "Send failed，请检查短信配置是否正确": "Send failed, please check SMS configuration",
    "No specified special found": "No specified special found",
    "No specified tags found": "No specified tags found",
    "用户名已经被占用": "Username already exists",
    "昵称已经被占用": "Nickname already exists",
    "该手机号已经占用": "Mobile number already in use",
    "手机号不存在": "Mobile number does not exist",
    "邮箱不存在": "Email does not exist",
    "邮箱已经被占用": "Email already in use",
    "密码必须是6到30个字符": "Password must be 6 to 30 characters",
    "用户未找到": "User not found",
    "登录成功": "Logged in successful",
    "退出登录成功": "Logout successful",
    "注册成功": "Sign up successful",
    "用户中心已关闭": "User center already closed",
    "用户名已存在": "Username already exists",
    "昵称已存在": "Nickname already exists",
    "邮箱已存在": "Email already exists",
    "手机号已存在": "Mobile number already exists",
    "密码重置成功": "Reset password successful",
    "操作失败": "Operation failed",
    "无效参数": "Invalid parameters",
}


def replace_text(content):
    # 匹配单引号或双引号内的字符串
    pattern = re.compile(r"""(['"])(.*?)\1""", re.DOTALL)

    def repl(m):
        quote = m.group(1)
        text = m.group(2)
        # 精准匹配整个字符串替换
        if text in replace_map:
            return f"{quote}{replace_map[text]}{quote}"
        else:
            return m.group(0)

    return pattern.sub(repl, content)

def process_file(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    new_content = replace_text(content)
    if new_content != content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"Replaced in: {filepath}")
    else:
        print(f"No changes: {filepath}")

def main():
    for root, dirs, files in os.walk('.'):
        for file in files:
            if file.endswith('.php'):
                full_path = os.path.join(root, file)
                process_file(full_path)

if __name__ == '__main__':
    main()
